<!DOCTYPE html>
<html lang="en">
<head>
    <!--引入Vue.js-->
    <script src="js/plugins/vue/dist/vue.js"></script>
    <!--引入axios.js-->
    <script src="js/plugins/axios/dist/axios.js"></script>
    <!--引入common.js中配置axios全局引用和基础路径-->
    <script src="js/common.js"></script>

    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="app"></div>
    <script type="text/javascript">
        new Vue({
            el: '#app',
            mounted(){
                //页面一加载就获取地址栏?后面的字符串
                var url = location.href;
                //调用方法返回一个json对象再获取对象里面code属性值
                var code = param2JsonObj(url).code;
                //页面一加载就发送axios请求，并携带参数code传递给后端
                this.$http.get("/login/wechat/" + code).then(res => {
                    if (res.data.success){ //登录成功并且绑定过，直接免密登录
                        //1.提示
                        alert("登录成功！");

                        //2.将数据保存到localStorage - token、登录对象
                        var token = res.data.resultObj.token;
                        localStorage.setItem("token", token);
                        //后端传过来的是一个登录对象，将对象转为字符串存进localStorage中
                        var logininfo = JSON.stringify(res.data.resultObj.logininfo);
                        localStorage.setItem("logininfo", logininfo);

                        //3.跳转到首页
                        location.href = "/index.html";

                    }else { //表示没有绑定过
                        var access_token = res.data.resultObj.access_token;
                        var openid = res.data.resultObj.openid;


                        //2.跳转到绑定页面并携带参数access_token和openid
                        location.href = "/binder.html?accessToken="+access_token+"&openId="+openid;
                    }
                }).catch(res => {
                    alert("系统繁忙！请稍后重试！");
                })

            }
        })


    </script>


</body>
</html>